package com.movie.ticket.sale.assembly.config;


import com.movie.ticket.sale.moviecommon.common.Result;
import lombok.extern.slf4j.Slf4j;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @BelongsProject: spring-redis
 * @BelongsPackage: com.woniu.springredis
 * @Author: lyl
 * @CreateTime: 2020-11-23 17:31
 * @Description:
 */
@Slf4j
public class App {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired

    private RedissonClient redissonClient;
    @Autowired
    private UserRepo userRepo;
    //方便 存数据的  只 运行一次
    @GetMapping("init")
    public String initRedis(){
        //存放数据
        BoundHashOperations<String, Object, Object> girl = redisTemplate.boundHashOps("girl");
        Map<String,String> map = new HashMap<>();
        map.put("三上悠亚","100");
        map.put("美琪","37");
        map.put("穆婷婷","20");
        map.put("陈芊芊","18");
        map.put("雪精灵","50");
        girl.putAll(map);
        return "执行成功";
    }

    @GetMapping("poll")
    public Result pollTicket(@RequestParam("name")String name){

        //传入一个参数  选择为谁投票
        log.info("正在为：{}投票",name);
        if(name != null){
            BoundHashOperations<String, Object, Object> girl = redisTemplate.boundHashOps("girl");
                girl.increment(name,1);
        }

        return Result.success("执行成功");

    }
    @GetMapping("look")
    private Result getTickets(){
        //获取所有的key
        Map<Object, Object> girls = redisTemplate.opsForHash().entries("girl");

        return Result.success(girls);
    }


    //压缩域名
    @GetMapping("red")
    public Result reduce(@RequestParam("internet")String internet){
                //将一个网址用点拆分
            System.out.println(internet);
            String str1 = internet.replace("www.","");
             String str2 = str1.replace(".com","");

            //将长域名 和短域名对应存起来
            BoundHashOperations<String, Object, Object> internets = redisTemplate.boundHashOps("internets");
            internets.put(str2,internet);
            return Result.success("执行成功，"+internet+"已被压缩为"+str2+"存储");

    }
    //解压域名
    @GetMapping("tar")
    public Result tar(@RequestParam("internet")String internet){
        //通过k 获得 val
        BoundHashOperations<String, Object, Object> internets = redisTemplate.boundHashOps("internets");
        Object object = internets.get(internet);
        return Result.success(object);
    }




    @GetMapping("/lock")
    public String testLock(String lockKey){
        RLock rLock = redissonClient.getLock(lockKey);
        boolean locked = rLock.tryLock();
        log.info("加锁成功？{}",locked);
        try {

            TimeUnit.SECONDS.sleep(25);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

//        加锁成功后 解锁之前 对该资源进行操作  加锁时所存放的Key是共同约定对该资源进行操作的一个标记
        log.info("准备解锁{}",locked);
        rLock.unlock();
        log.info("解锁完成");
        return "success";

    }







}
